Imports System.Data

Public Class TargetAccounts
    Inherits DataSet
    Implements ITargetItems

    Private Const _properties As String = "cn,sn,givenname,SAMAccountName,mail,initials,company,department,physicaldeliveryofficename,title," & _
        "streetAddress,l,st,postalCode,telephoneNumber," & _
        "publicDelegates,publicDelegatesBL," & _
        "whenCreated,whenChanged,lastLogonTimestamp,modifyTimeStamp"
    Private Const _coreTableName As String = "accounts"
    Private Const _itemName As String = "accounts"

    Public Sub New()

        Refresh()

    End Sub

    Public ReadOnly Property ItemName() As String Implements ITargetItems.ItemName
        Get
            Return _itemName
        End Get
    End Property

    Public ReadOnly Property CoreTableName() As String Implements ITargetItems.CoreTableName
        Get
            Return _coreTableName
        End Get
    End Property

    Public ReadOnly Property DataSet() As DataSet Implements ITargetItems.DataSet
        Get
            Return Me
        End Get
    End Property

    Public Sub Refresh() Implements ITargetItems.Refresh

        If Me.Tables.Contains(_coreTableName) Then
            Me.Tables.Remove(_coreTableName)
        End If

        'add the target accounts
        Dim myAD As ADHelper = ADHelper.GetInstance

        Dim groupFilter As String
        If My.Settings.UserManagementTargetGroupCN.Length > 0 Then
            groupFilter = "CN=" & My.Settings.UserManagementTargetGroupCN & "," & My.Settings.LDAPOUDN
        Else
            groupFilter = ""
        End If

        Dim dt As DataTable = myAD.getAccounts(myAD.LDAPRoot, "user", groupFilter, _properties)
        dt.TableName = _coreTableName

        Me.Tables.Add(dt)

    End Sub

    Public Sub SetGrouping(ByVal GroupingAttribute As String) Implements ITargetItems.SetGrouping

        If Me.Tables.Contains("Grouping1") Then
            Me.Tables.Remove("Grouping1")
        End If

        Dim dv As DataView = New DataView(Me.Tables(_coreTableName))
        Dim dtGrouping As DataTable
        dtGrouping = DataSetHelper.selectDistinct(DataSetHelper.selectColumns(dv.ToTable, New String() {GroupingAttribute}))

        dtGrouping.TableName = "Grouping1"

        Me.Tables.Add(dtGrouping)

    End Sub

End Class
